<template>
	<view class="container">
		<view class="form-item">
			<text class="label">医生姓名</text>
			<input class="input" v-model="form.name" placeholder="请输入医生姓名" />
		</view>
		<view class="form-item">
			<text class="label">学历</text>
			<input class="input" v-model="form.education" placeholder="请输入学历" />
		</view>
		<view class="form-item">
			<text class="label">毕业院校</text>
			<input class="input" v-model="form.graduationSchool" placeholder="请输入毕业院校" />
		</view>
		<view class="form-item">
			<text class="label">医生证书名称</text>
			<input class="input" v-model="form.certificateName" placeholder="请输入医生证书名称" />
		</view>
		<view class="form-item">
			<text class="label">医生证书上传</text>
			<button class="upload-btn" @click="selectFile">
				选择证书文件
			</button>
			<view v-if="form.certificateFile">
				已选择文件: {{ form.fileName }}
			</view>
		</view>
		<button class="submit-btn" type="primary" @click="submitApplication">
			提交申请
		</button>
	</view>
</template>

<script>
	// 假设 applyDoctor 接口函数已经导入
	import {
		applyDoctor
	} from '@/api/index.js';

	export default {
		data() {
			return {
				form: {
					name: '小明',
					education: '大专',
					graduationSchool: '清华',
					certificateName: '医生证',
					certificateFile: '',
					fileName: ''
				},
				pdfInfo: [],
				userInfo: {}
			};
		},
		async onLoad(option) {
			let uInfo = uni.getStorageSync('mini-userinfo')
			if (uInfo) {
				this.userInfo = uInfo
			}
		},
		methods: {
			// 上传证书文件

			selectFile() {
				if (this.pdfInfo[0]) {
					uni.showToast({
						title: '只能上传一个文件',
						icon: 'none'
					})
					return
				}
				this.pdfInfo = []
				wx.chooseMessageFile({
					count: 1,
					// type:'file',
					extension: ['.pdf'],
					success: (res) => {
						uni.showLoading({
							title: '上传中',
						})
						console.log(res, '选择文件成功后')
						if (res.tempFiles[0].size > 52428800) {
							return uni.showToast({
								title: '文件不能大于50MB',
								icon: 'none'
							})
						}
						if (res.tempFiles[0].name.split('.')[1] !== 'pdf') {
							return uni.showToast({
								title: '只能上传pdf文件',
								icon: 'none'
							})
						}
						const tempFilePaths = res.tempFiles
						uni.uploadFile({
							url: `${uni.$http.baseUrl}/api/upload/file`, //上传图片接口
							filePath: tempFilePaths[0].path,
							name: 'file',
							// header: { //上传文件请求头数据
							// 	'ft-auth': 'bearer ' + this.userData.access_token,
							// 	authorization: this.userData.authorization
							// },
							success: res => {
								console.log(JSON.parse(res.data), '成功后的结果');
								let data = JSON.parse(res.data)
								this.pdfInfo.push(data.data)
								this.form.certificateFile = data.data.fileUrl
								this.form.fileName = data.data.filename
							},
							fail: err => {
								uni.showToast({
									title: '文件上传失败',
									icon: 'none'
								})
							},
							complete: () => {
								uni.hideLoading()
							}
						})
					}
				});
			},
			// 提交申请
			async submitApplication() {
				// 简单表单验证
				if (!this.form.name || !this.form.education || !this.form.graduationSchool || !this.form
					.certificateName || !this.form.certificateFile) {
					uni.showToast({
						title: '请填写完整信息',
						icon: 'none'
					});
					return;
				}

				const params = {
					name: this.form.name,
					education: this.form.education,
					graduationSchool: this.form.graduationSchool,
					certificateName: this.form.certificateName,
					certificateFile: this.form.certificateFile,
				}

				try {
					const res = await applyDoctor(this.userInfo.id, params);
					console.log(res,'..................');
					if (res.statusCode===200) {
						uni.showToast({
							title: '申请提交成功',
							icon: 'success'
						});
						uni.navigateBack()
					} else {
						uni.showToast({
							title: res.message || '申请提交失败',
							icon: 'none'
						});
					}
				} catch (err) {
					console.error('提交申请失败:', err);
					uni.showToast({
						title: '申请提交失败',
						icon: 'none'
					});
				}
			}
		}
	};
</script>

<style scoped>
	.container {
		padding: 20rpx;
	}

	.form-item {
		margin-bottom: 30rpx;
	}

	.label {
		display: block;
		margin-bottom: 10rpx;
		font-size: 28rpx;
		color: #333;
	}

	.input {
		width: 100%;
		height: 80rpx;
		padding: 0 20rpx;
		border: 1rpx solid #ddd;
		border-radius: 8rpx;
		font-size: 28rpx;
	}

	.upload-btn {
		margin-top: 10rpx;
	}

	.submit-btn {
		margin-top: 50rpx;
	}
</style>